;
var user_index_reg = {
    init:function(){
        this.is_club = $(".hide_wrap input[name=is_club]").val();
        this.province_infos = {};
        this.intervalid = null;
        this.timeCount = 60;
        this.eventBind();
        this.getUserDevices();
        $( $('.version_des .each').get( this.is_club ) ).click();
    },
    eventBind:function(){
        var that = this;
        $(".user_footer").hide();
        $("#reg .doreg").click(function(){
            if($(this).hasClass("secondary")){
                alert("正在处理!!请不要重复提交");
                return;
            }

            if(!that.inputCheck()){
                return;
            }
            $(this).addClass("secondary");
            that.dataSubmit();
            //$("#reg").submit();
        });
        $("#reg .get_captcha").click(function(){
            var mobile = $("#reg input[name=mobile]").val();
            var public_key = $("#reg input[name=public_key]").val();
            var timestamp = $("#reg input[name=timestamp]").val();

            if($(this).hasClass("secondary")){
                return false;
            }

            if( mobile.length <= 0 ||
                !/^[1-9]\d{10}$/.test( mobile ) )
            {
                alert("请输入符合要求的手机号码！");
                return false;
            }
            that.lightenOrDisabled("disabled");
            $.ajax({
                url:common_ops.getUrlPrefix() + "/user/get_captcha",
                type:'POST',
                data:{'mobile':mobile,'public_key':public_key,'timestamp':timestamp},
                dataType:'json',
                success:function(res){
                    if(res.code == 200){
                        that.lightenOrDisabled("countdown");
                    }else{
                        alert(res.msg);
                        that.lightenOrDisabled("light");
                    }
                }
            });
        });
        $("#province_id").change(function(){
            var id = $(this).val();
            if(id <= 0){
                return;
            }
            for(var key in that.province_infos){
                if(key == id){
                    that.province_cascade();
                    return;
                }
            }
            $.ajax({
                url :common_ops.getUrlPrefix()+'/user/cascade',
                data:{'id':id},
                dataType:'json',
                async: false,
                success:function(res){
                    if(res.code == 200){
                        that.province_infos[id] = res.data;
                        that.province_cascade();
                    }else{
                        alert(res.msg);
                    }
                }
            })
        });

        $("#city_id").change(function(){
            that.city_cascade();
        });

        $('.version_des .each').click(function(){
            $(this).addClass('current').siblings('.each').removeClass('current');
            var classify = $(this).attr('classify');
            var text = $(this).find('h2').text();
            var classify_type_mapping = eval('('+$("[name=classify_type_mapping]").val()+')');
            var classify_type_mapping_arr = [];
            var html = '<option value="0">请选择行业</option>';
            $('.reg_box h3').text( '申请使用' + text);
            if( classify == 'club' ){
                $('.industry').hide();
                $('.doreg').html("申请使用");
                classify_type_mapping_arr = classify_type_mapping['club'];
            }else if(classify == 'meiye'){
                $('.industry').show();
                $('.doreg').html("申请使用");
                classify_type_mapping_arr = classify_type_mapping['meiye'];
            }else{
                $('.industry').show();
                $('.doreg').html("申请试用");
                classify_type_mapping_arr = classify_type_mapping['studio'];
            }
            for(var idx in classify_type_mapping_arr){
                html += '<option value="'+idx+'"'+(classify == "club"?"selected":"")+'>'+classify_type_mapping_arr[idx]['name']+'</option>';
            }
            $("[name=classify_type_id]").html(html)
        });
    },
    inputCheck:function(){

        if( $("#reg #classify_type_id").val() <=0 ){
            alert("请选择行业");
            return false;
        }

        var brand_shop_name = $("#reg input[name=brand_shop_name]").val();
        if( brand_shop_name.length <= 0  || brand_shop_name.length > 50 ){
            alert("请输入您需要管理的场馆名称并且不能多于50个字符");
            return false;
        }

        if( $("#reg #province_id").val() <=0){
            alert("请选择省份!");
            return false;
        }

        if($("#reg #city_id").val() <=0){
            alert("请选择城市!");
            return false;
        }
        if($("#reg #district_id").val() <=0){
            alert("请选择区域!");
            return false;
        }

        if($('#reg input[name=brand_shop_address]').val().length <= 0 ){
            alert("请输入您场馆详细地址!");
            return false;
        }

        if($('#reg input[name=brand_contact_nickname]').val().length <= 0 ){
            alert("请输入场馆的负责人!");
            return false;
        }


        if( $("#reg input[name=mobile]").val().length <= 0 ||
            !/^[1-9]\d{10}$/.test($("#reg input[name=mobile]").val()) )
        {
            alert("请输入符合要求的手机号码！");
            return false;
        }

        if ( $("#reg input[name=captcha]").val().length <= 0 ){
            alert("请输入验证码！");
            return false;
        }

        var agreement_status = $("#reg  input[name=yes_agreement]:checked").val();
        if( parseInt( agreement_status ) != 1 ){
            alert("请先同意 三体云动服务协议！");
            return false;
        }

        return true;
    },
    dataSubmit:function(){
        var data = {
            'classify_type_id':$("#reg #classify_type_id").val(),
            'brand_shop_name':$("#reg input[name=brand_shop_name]").val(),
            'province_id':$("#reg #province_id").val(),
            'city_id':$("#reg #city_id").val(),
            'district_id':$("#reg #district_id").val(),
            'brand_shop_address':$('#reg input[name=brand_shop_address]').val(),
            'brand_contact_nickname':$('#reg input[name=brand_contact_nickname]').val(),
            'mobile':$("#reg input[name=mobile]").val(),
            'captcha':$("#reg input[name=captcha]").val()
        };

        $.ajax({
            url: "",
            type:'POST',
            data:data,
            dataType:'json',
            success:function( res ){
                $("#reg .doreg").removeClass("secondary");
                alert( res.msg );
                if( res.code == 200 ){
                    window.location.href = res.data.url;
                }
            }
        });

    },
    lightenOrDisabled:function( type ){
        if( type == "disabled" ){
            $("#reg .get_captcha").addClass("secondary");
        }else if( type == "light" ){
            $("#reg .get_captcha").removeClass("secondary");
        }else if( type == "countdown" ){
            $("#reg .get_captcha").addClass("secondary");
            this.intervalid = setInterval(this.setCaptchaTips, 1000);
        }
    },
    setCaptchaTips:function(){//倒计时提示
        user_index_reg.timeCount--;
        if(user_index_reg.timeCount <= 0 ){
            clearInterval(user_index_reg.intervalid);
            user_index_reg.lightenOrDisabled("light");
            $("#reg .get_captcha").html("获取验证码");
            user_index_reg.timeCount = 60;
            return;
        }
        $("#reg .get_captcha").html(user_index_reg.timeCount + " 重新获取");

    },
    province_cascade:function(){
        var id = $("#province_id").val();
        var province_info = this.province_infos[id];
        var city_info = province_info.city;
        if(id<=0){
            return;
        }
        $("#city_id").html("");
        $("#city_id").append("<option value='0'>请选择</option>");
        for(var idx in city_info){
            if( parseInt($("#city_id_id_before").val()) == city_info[idx]['id']){
                $("#city_id").append("<option value='"+city_info[idx]['id']+"' selected='select'>"+city_info[idx]['name']+"</option>");
                continue;
            }
            $("#city_id").append("<option value='"+city_info[idx]['id']+"'>"+city_info[idx]['name']+"</option>");
        }
    },
    city_cascade:function(){
        var id = $("#province_id").val();
        var province_info = this.province_infos[id];
        var city_id =$("#city_id").val();
        var district_info = province_info.district[city_id];
        if(id<=0 || city_id<=0){
            return;
        }
        $("#district_id").html("");
        $("#district_id").append("<option value='0'>请选择</option>");
        for(var idx in district_info){
            if( parseInt( $("#district_id_before").val() ) == district_info[idx]['id'] ){
                $("#district_id").append("<option value='"+district_info[idx]['id']+"' selected='select'>"+district_info[idx]['name']+"</option>");
                continue;
            }
            $("#district_id").append("<option value='"+district_info[idx]['id']+"'>"+district_info[idx]['name']+"</option>");
        }
    },
    getUserDevices:function(){
      var ua = navigator.userAgent,
          android = ua.match(/(Android)\s+([\d.]+)/),
          ios = ua.match(/(iPad).*OS\s([\d_]+)/) || ua.match(/(iPhone\sOS)\s([\d_]+)/);
        if(android || ios){
          $("head").append('<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">');
          $("select[name=province_id] option:eq(0)").text("省份");
          $("select[name=city_id] option:eq(0)").text("城市");
          $("select[name=district_id] option:eq(0)").text("县/区");
        }
    }
};
$(document).ready(function(){
    user_index_reg.init();
    $(".wrap").addClass('min_reg');
});
